package cn.flightcloud.auth.config.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class Oauth2Interceptor extends HandlerInterceptorAdapter {

	private TokenStore tokenStore;

	public Oauth2Interceptor(TokenStore tokenStore) {
		this.tokenStore = tokenStore;
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String accessToken = request.getParameter("access_token");
		if (StringUtils.isEmpty(accessToken)) {
			return false;
		}
		OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(accessToken);
		if (oAuth2Authentication == null) {
			return false;
		}
		SecurityContextHolder.getContext().setAuthentication(oAuth2Authentication);
		return true;
	}
}
